package com.myl.api.controller.sys;

import com.myl.core.basic.BaseController;
import com.myl.core.domain.entity.SysDept;
import com.myl.api.service.ISysDeptService;
import com.myl.core.domain.model.TablePageList;
import com.myl.core.utils.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 控制器
 * </p>
 *
 * @author Myl <ixiaomu@qq.com>
 * @date 2024-01-10 11:21:21
 */
@Tag(name = "部门管理")
@Slf4j
@RestController
@RequestMapping("/sys/dept")
public class DeptController extends BaseController {

    @Autowired
    private ISysDeptService iSysDeptService;

    /**
     * 列表
     *
     * @author Myl <ixiaomu@qq.com>
     * @date 2024-01-10 11:21:21
     */
    @Operation(summary = "列表")
    @GetMapping("list")
    public ResponseResult<?> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页显示数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "关键词") @RequestParam(defaultValue = "") String keyword
    ) {
        TablePageList data = iSysDeptService.getListWithPage(current, pageSize, keyword);
        return ResponseResult.success(data);
    }

    /**
     * 详情
     *
     * @author Myl <ixiaomu@qq.com>
     * @date 2024-01-10 11:21:21
    */
    @Operation(summary = "获取详情")
    @GetMapping("info")
    public ResponseResult<?> getInfo(@PathVariable(value = "id", required = false) Long id) {
        SysDept info = iSysDeptService.getById(id);
        return ResponseResult.success(info);
    }

    /**
     * 新增
     *
     * @author Myl <ixiaomu@qq.com>
     * @date 2024-01-10 11:21:21
     */
    @Operation(summary = "新增 SysDept")
    @PostMapping(value = "add")
    public ResponseResult<?> add(
            @RequestBody() SysDept entity
    ) {
        SysDept info = iSysDeptService.saveOrUpdateById(entity);
        return ResponseResult.success(info);
    }

    /**
     * 编辑
     *
     * @author Myl <ixiaomu@qq.com>
     * @date 2024-01-10 11:21:21
     */
    @Operation(summary = "编辑")
    @PostMapping("edit")
    public ResponseResult<?> edit(
            @RequestBody() SysDept sysDept
    ) {
        Assert.notNull(sysDept.getId(), "ID 不能为空");
        SysDept info = iSysDeptService.saveOrUpdateById(sysDept);
        return ResponseResult.success(info);
    }

    /**
     * （批量）删除 部门表
     *
     * @author Myl <ixiaomu@qq.com>
     * @date 2024-01-10 11:21:21
     */
    @Operation(summary = "（批量）删除")
    @DeleteMapping("delete")
    public ResponseResult<?> delete(
            @RequestBody() List<Integer> ids
    ) {
        iSysDeptService.deleteBatchByIds(ids);
        return ResponseResult.success("操作成功");
    }

}
